<script>
/*
 * detect keycode no support
 */
function detectspecialkeys(e){
    if ((e.keyCode>=8&&e.keyCode<=46) || ((e.keyCode>=65&&e.keyCode<=93)) || ((e.keyCode>=106)))
    {
        return false;
    }
    else{
        return true;
    }
}
/*
 * code here
 */
$(document).ready(function() {
    /*
     * onchange working to holiday
     */
    $( "select[name='DateType']" ).change(function() {
        var id = $(this).val();
        var idCard = $(this).attr('nameid');
        if(id=='1'){
            var Url = "<?php echo base_url()."timecard/changeworking"; ?>";
            onChangeWorking(idCard,Url);
        }
        else{
            onChangeHoliday(IDCARD);
        }
    });
});

</script>
<script src="<?php echo base_url();?>public/sites/js/caculation.js" type="text/javascript"></script>
<div class="timecard">
    <h2><?php echo $this->lang->line('tc_title');?></h2>
    <div class="clr"></div>
    <h3><?php echo $employee; ?></h3>
    <div class="clr"></div>
	<?php
        if(isset($message)){
            echo "<label for='disabledSelect' style='color:red;'>".$message."</label>";
        }
        if(isset($data) && count($data)>0){
        ?>
        <form action="" method="post">   
        <div id="content-1" class="contentscroll" > 
            <table border="1">
                <tr id="th">
                    <th width="3%"><?php echo $this->lang->line('tc_ID'); ?></th>
                    <th><?php echo $this->lang->line('tc_Date'); ?></th>
                    <th><?php echo $this->lang->line('tc_Weekday'); ?></th>
                    <th><?php echo $this->lang->line('tc_Working'); ?></th>
                    <th><?php echo $this->lang->line('tc_ShiftInfo'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualArrivalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualLeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_TrimmedArrivalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_TrimmedLeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WorkingHours'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime1'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime2'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime3'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualWorkHours'); ?></th>
                    <th><?php echo $this->lang->line('tc_LeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayNormalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayLateOverTime'); ?></th>        
                    <th><?php echo $this->lang->line('tc_WeekendOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekendLateOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_HolidayOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_HolidayLateOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_LateArrival'); ?></th>
                    <th><?php echo $this->lang->line('tc_EarlyArrival'); ?></th>
                <tr>        
                <?php
		$count = 1;
                foreach($data as $row){
                ?>
                <tr id="<?php echo $row['TimeCardId']; ?>" style="<?php if($row['HighLight']==1){echo "background:#f0ad4e;"; }?>"  >
                        <td><?php echo $count; ?></td>
                        <td id="Date"><?php echo date('Y/m/d',strtotime($row['Date'])); ?></td>
                        <td id="WeekDay"><?php echo $row['WeekDay']; ?></td>
                        <td>
                            <select id="DateType" name="DateType" nameid="<?php echo $row['TimeCardId']; ?>" >
                                <option value="1" <?php if($row['DateType']==1){echo "selected='selected'";}?>>
                                    <?php echo $this->lang->line('tc_Working'); ?>
                                </option>
                                <option value="2" <?php if($row['DateType']==2){echo "selected='selected'";}?>>
                                    <?php echo $this->lang->line('tc_Holiday'); ?>
                                </option>
                                <option value="0" <?php if($row['DateType']==0){echo "selected='selected'";}?>>
                                    <?php echo $this->lang->line('tc_Undifine'); ?>
                                </option>                                
                            </select>
                        </td>
                        <td>
                            <select id="ShiftType" name="ShiftType">
                            <?php
                                foreach($shiftInfoConfig as $rowShift){
                                    ?>
                                    <option value="<?php echo $rowShift['ShiftId'];?>" <?php if($row['DateType']!=0&&$rowShift['ShiftId']==$row['ShiftId']){echo "selected='selected'"; } ?>>
                                        <?php echo $rowShift['ShiftName'];?>
                                    </option>                                    
                                    <?php
                                }
                            ?>
                            <option value="0" <?php if($row['DateType']==0 ||$row['DateType']==2){echo "selected='selected'"; } ?>>
                                <?php echo $this->lang->line('tc_Undifine'); ?>
                            </option>                                    
                            </select>
                        </td>
                        <td id="ActualArrivalTime" ><?php echo $row['ActualArrivalTime']; ?></td>
                        <td id="ActualLeaveTime" ><?php echo $row['ActualLeaveTime']; ?></td>
                        <td>
                            <div class="changwidth">
                                <?php
                                    $H1 = date('H', strtotime($row['TrimmedArrivalTime']));
                                    $M1 = date('i', strtotime($row['TrimmedArrivalTime']));
                                ?>
                                <input id="ArrivalTime1"  class="textinput integer" type="text" name="ArrivalTime1[<?php echo $count;?>]" value="<?php echo $H1; ?>" maxlength="2" nameid="<?php echo $row['TimeCardId']; ?>" />:
                                <input id="ArrivalTime2" class="textinput integer" type="text"  name="ArrivalTime2[<?php echo $count;?>]" value="<?php echo $M1; ?>" maxlength="2" nameid="<?php echo $row['TimeCardId']; ?>" />
                                <input id="ORGArrivalTime1"  type="hidden" value="<?php echo $H1; ?>"  />
                                <input id="ORGArrivalTime2"  type="hidden"   value="<?php echo $M1; ?>" />                                
                            </div>
                        </td>
                        <td>
                            <div class="changwidth">
                            <?php
                                $H2 = date('H', strtotime($row['TrimmedLeaveTime']));
                                $M2 = date('i', strtotime($row['TrimmedLeaveTime']));
                            ?>                            
                            <input id="LeaveTime1" class="textinput integer" type="text" name="LeaveTime1[<?php echo $count;?>]" value="<?php echo $H2; ?>" maxlength="2" nameid="<?php echo $row['TimeCardId']; ?>" />:
                            <input id="LeaveTime2" class="textinput integer" type="text"  name="LeaveTime2[<?php echo $count;?>]" value="<?php echo $M2; ?>" maxlength="2" nameid="<?php echo $row['TimeCardId']; ?>" />
                            <input id="ORGLeaveTime1" type="hidden" value="<?php echo $H2; ?>" />
                            <input id="ORGLeaveTime2" type="hidden" value="<?php echo $M2; ?>" />                            
                            </div>
                        </td>
                        <td id="WorkingHours">
                            <!-- time working -->
                            <?php
                                if($M2 < $M1){
                                    $Mute = -( $M2 - $M1 );
                                    $Hour = $H2 - $H1 - 1;
                                }
                                else{
                                    $Mute = $M2 - $M1;
                                    $Hour = $H2 - $H1;
                                }
                                if($Mute==0){
                                    echo $Hour.":".$Mute."0";
                                }
                                else{
                                    echo $Hour.":".$Mute;
                                }
                            ?>
                            <!-- end time working -->
                        </td>
                        <td>
                            <div class="changwidth">
                            <?php
                                $restime1 = explode('.', $row['ResTime1']);
                            ?>                            
                            <input id="ResTime11" type="text" class="textinput integer" name="ResTime11[<?php echo $count;?>]" value="<?php echo $restime1[0]; ?>" maxlength="1" nameid="<?php echo $row['TimeCardId']; ?>" />:
                            <?php 
                            $getrestime1 = array();
                            echo "<input id='ResTime12' class='textinput integer' type='text'  name='ResTime12[".$count."]' value='";
                                if(isset($restime1[1])&& $restime1[1]!=""){
                                    $restime1[1] = "0.".$restime1[1];
                                    $getrestime1 = explode('.', $restime1[1]*60);
                                    echo $getrestime1[0]; 
                                }
                                else{
                                    echo "00"; 
                                }
                            echo "' maxlength='2' nameid='".$row['TimeCardId']."' /> ";							
                            ?>
                            </div>
                        </td>
                        <td>
                            <div class="changwidth">
                            <?php
                                $restime2 = explode('.', $row['ResTime2']);
                            ?>                            
                            <input id="ResTime21" class="textinput integer" type="text" name="ResTime21[<?php echo $count;?>]" value="<?php echo $restime2[0]; ?>" maxlength="1" nameid="<?php echo $row['TimeCardId']; ?>" />:
                            <?php 
                            echo "<input id='ResTime22' class='textinput integer' type='text'  name='ResTime22[".$count."]' value='";
                                if(isset($restime2[1])&& $restime2[1]!=""){
                                    $restime2[1] = "0.".$restime2[1];
                                    $getrestime2 = explode('.', $restime2[1]*60);
                                    echo $getrestime2[0]; 
                                }
                                else{
                                    echo "00"; 
                                }
                            echo "' maxlength='2' nameid='".$row['TimeCardId']."' /> ";						
                            ?>
                            </div>
                        </td>
                        <td>
                            <div class="changwidth">
                            <?php
                                $restime3 = explode('.', $row['ResTime3']);
                            ?>                            
                            <input id="ResTime31" class="textinput integer" type="text" name="ResTime31[<?php echo $count;?>]" value="<?php echo $restime3[0]; ?>" maxlength="1" nameid="<?php echo $row['TimeCardId']; ?>" />:
                            <?php 
                            echo "<input id='ResTime32' class='textinput integer' type='text'  name='ResTime32[".$count."]' value='";
                                if(isset($restime3[1])&& $restime3[1]!=""){
                                    $restime3[1] = "0.".$restime3[1];
                                    $getrestime3 = explode('.', $restime3[1]*60);
                                    echo $getrestime3[0];  
                                }
                                else{
                                    echo "00"; 
                                }
                            echo "' maxlength='2' nameid='".$row['TimeCardId']."' /> ";						
                            ?> 
                            </div>
                        </td>
                        <td id="ActualWorkHours">
                            <?php
                                $ActualWorkHours = explode('.', $row['ActualWorkHours']);
                                if(isset($ActualWorkHours[1])&& $ActualWorkHours[1]!=""){
                                    $ActualWorkHours[1] = "0.".$ActualWorkHours[1];
                            ?>
                                     <input type="text" class="inputtext" name="ActualWorkHours10[<?php echo $count;?>]" id="ActualWorkHours10" value="<?php echo $ActualWorkHours[0].":".$ActualWorkHours[1]*60; ?>" readonly="readonly" />
                            <?php
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="ActualWorkHours10[<?php echo $count;?>]" id="ActualWorkHours10" value="<?php echo $ActualWorkHours[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php
                                }                            
                            ?>
                        </td>
                        <td id="LeaveTime">
                            &nbsp;
                        </td>
                        <td id="WeekdayNormalTime">
                            <?php
                                $WeekdayNormalTime = explode('.', $row['WeekdayNormalTime']);
                                if(isset($WeekdayNormalTime[1])&& $WeekdayNormalTime[1]!=""){
                                    $WeekdayNormalTime[1] = "0.".$WeekdayNormalTime[1];
                                    $getWeekdayOvertime = explode('.', $WeekdayNormalTime[1]*60);
                                ?>
                                <input type="text" class="inputtext" name="WeekdayNormalTime10[<?php echo $count;?>]" id="WeekdayNormalTime10" value="<?php echo $WeekdayNormalTime[0].":".$getWeekdayOvertime[0]; ?>" readonly="readonly" />
                                <?php 
                                }
                                else{
				?>
                                <input type="text" class="inputtext" name="WeekdayNormalTime10[<?php echo $count;?>]" id="WeekdayNormalTime10" value="<?php echo $WeekdayNormalTime[0].":"."00"; ?>" readonly="readonly" />                                    
                                <?php
                                }  
                            ?>                                                
                        </td>
                        <td id="WeekdayOvertime">
                            <?php
                                $WeekdayOvertime = explode('.', $row['WeekdayOvertime']);
                                if(isset($WeekdayOvertime[1])&& $WeekdayOvertime[1]!=""){
                                    $WeekdayOvertime[1] = "0.".$WeekdayOvertime[1];
                                    $getWeekdayOvertime = explode('.', $WeekdayOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="WeekdayOvertime10[<?php echo $count;?>]" id="WeekdayOvertime10" value="<?php echo $WeekdayOvertime[0].":".$getWeekdayOvertime[0]; ?>" readonly="readonly" />                                    
                                    <?php
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="WeekdayOvertime10[<?php echo $count;?>]" id="WeekdayOvertime10" value="<?php echo $WeekdayOvertime[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php
                                }                            
                            ?>                        
                        </td>
                        <td id="WeekdayLateOvertime">
                            <?php
                                $WeekdayLateOvertime = explode('.', $row['WeekdayLateOvertime']);
                                if(isset($WeekdayLateOvertime[1])&& $WeekdayLateOvertime[1]!=""){
                                    $WeekdayLateOvertime[1] = "0.".$WeekdayLateOvertime[1];
                                    $getWeekdayLateOvertime = explode('.', $WeekdayLateOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="WeekdayLateOvertime10[<?php echo $count;?>]" id="WeekdayLateOvertime10" value="<?php echo $WeekdayLateOvertime[0].":".$getWeekdayLateOvertime[0]; ?>" readonly="readonly" />
                                    <?php
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="WeekdayLateOvertime10[<?php echo $count;?>]" id="WeekdayLateOvertime10" value="<?php echo $WeekdayLateOvertime[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php 
                                }                            
                            ?>                                                 
                        </td>
                        <td id="WeekendOvertime">
                            <?php
                                $WeekendOvertime = explode('.', $row['WeekendOvertime']);
                                if(isset($WeekendOvertime[1])&& $WeekendOvertime[1]!=""){
                                    $WeekendOvertime[1] = "0.".$WeekendOvertime[1];
                                    $getWeekendOvertime = explode('.', $WeekendOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="WeekendOvertime10[<?php echo $count;?>]" id="WeekendOvertime10" value="<?php echo $WeekendOvertime[0].":".$getWeekendOvertime[0]; ?>" readonly="readonly" />                                    
                                    <?php  
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="WeekendOvertime10[<?php echo $count;?>]" id="WeekendOvertime10" value="<?php echo $WeekendOvertime[0].":"."00"; ?>" readonly="readonly" />                                     
                                    <?php 
                                }                            
                            ?>                       
                        </td>
                        <td id="WeekendLateOvertime">
                            <?php
                                $WeekendLateOvertime = explode('.', $row['WeekendLateOvertime']);
                                if(isset($WeekendLateOvertime[1])&& $WeekendLateOvertime[1]!=""){
                                    $WeekendLateOvertime[1] = "0.".$WeekendLateOvertime[1];
                                    $getWeekendLateOvertime = explode('.', $WeekendLateOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="WeekendLateOvertime10[<?php echo $count;?>]" id="WeekendLateOvertime10" value="<?php echo $WeekendLateOvertime[0].":".$getWeekendLateOvertime[0]; ?>" readonly="readonly" />
                                    <?php 
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="WeekendLateOvertime10[<?php echo $count;?>]" id="WeekendLateOvertime10" value="<?php echo $WeekendLateOvertime[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php
                                }                            
                            ?>                                               
                        </td>
                        <td id="HolidayOvertime">
                            <?php
                                $HolidayOvertime = explode('.', $row['HolidayOvertime']);
                                if(isset($HolidayOvertime[1])&& $HolidayOvertime[1]!=""){
                                    $HolidayOvertime[1] = "0.".$HolidayOvertime[1];
                                    $getHolidayOvertime = explode('.', $HolidayOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="HolidayOvertime10[<?php echo $count;?>]" id="HolidayOvertime10" value="<?php echo $HolidayOvertime[0].":".$getHolidayOvertime[0]; ?>" readonly="readonly" />                                    
                                    <?php  
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="HolidayOvertime10[<?php echo $count;?>]" id="HolidayOvertime10" value="<?php echo $HolidayOvertime[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php
                                }                            
                            ?>                                                  
                        </td>
                        <td id="HolidayLateOvertime">
                            <?php
                                $HolidayLateOvertime = explode('.', $row['HolidayLateOvertime']);
                                if(isset($HolidayLateOvertime[1])&& $HolidayLateOvertime[1]!=""){
                                    $HolidayLateOvertime[1] = "0.".$HolidayLateOvertime[1];
                                    $getHolidayLateOvertime = explode('.', $HolidayLateOvertime[1]*60);
                                    ?>
                                    <input type="text" class="inputtext" name="HolidayLateOvertime10[<?php echo $count;?>]" id="HolidayLateOvertime10" value="<?php echo $HolidayLateOvertime[0].":".$getHolidayLateOvertime[0]; ?>" readonly="readonly" />
                                    <?php
                                }
                                else{
                                    ?>
                                    <input type="text" class="inputtext" name="HolidayLateOvertime10[<?php echo $count;?>]" id="HolidayLateOvertime10" value="<?php echo $HolidayLateOvertime[0].":"."00"; ?>" readonly="readonly" />                                    
                                    <?php 
                                }                            
                            ?>                                              
                        </td>
                        <td id="LateArrival">
                            <?php
                                $LateArrival = explode('.', $row['LateArrival']);
                                if(isset($LateArrival[1])&& $LateArrival[1]!=""){
                                    $LateArrival[1] = "0.".$LateArrival[1];
                                    $getLateArrival = explode('.', $LateArrival[1]*60);
                                    echo $LateArrival[0].":".$getLateArrival[0]; 
                                }
                                else{
                                    echo $LateArrival[0].":"."00"; 
                                }                            
                            ?>                                              
                        </td>
                        <td id="EarlyArrival">
                            <?php
                                $EarlyLeave = explode('.', $row['EarlyLeave']);
                                if(isset($EarlyLeave[1])&& $EarlyLeave[1]!=""){
                                    $EarlyLeave[1] = "0.".$EarlyLeave[1];
                                    $getEarlyLeave = explode('.', $EarlyLeave[1]*60);
                                    echo $EarlyLeave[0].":".$getEarlyLeave[0]; 
                                }
                                else{
                                    echo $EarlyLeave[0].":"."00"; 
                                }                            
                            ?>                                               
                        </td>
                        <?php
                            $mTCM = $this->load->model('mod_tcmanipulation/Mmod_tcmanipulation');                         
                            $overTime =$mTCM->isCheckOvertime($row['EmployeeId'],$row['Date']);
                            if( $overTime > 0 ){
                                echo "<input id='OverTime' type='hidden' name='OverTime' value='1' />";
                            }
                            else{
                                echo "<input id='OverTime' type='hidden' name='OverTime' value='0' />";
                            }
                            $Restime10 = 0;
                            $registrationDates = $mTCM->getLeaveRegistrationEmployeeID($row['EmployeeId'],$row['Date']); 
                            if(isset($registrationDates)&& !empty($registrationDates)){
                            foreach($registrationDates as $registrationDate){
                                // case 1 leave registration is Paid
                                if($mTCM->isCheckLeaveRegistrationPaid($registrationDate['LeaveTypeId'],$row['EmployeeId'],$row['Date'])== false){
                                    // caculation resetime 10
                                    $Restime10 = $mTCM->resTime10($row['TrimmedArrivalTime'],$row['TrimmedLeaveTime'],$row['StartRestTime1'],$row['EndRestTime1'],$row['StartTime'],$row['EndTime'],$registrationDate['StartTime'],$registrationDate['EndTime']);                                                            
                                }
                             }                                                    
                        }
							
                        ?>
                        <input type="hidden" id="blockTime" name="blockTime" value="<?php echo $row['RoundedTimeBlock']; ?>" />
                        <input type="hidden" id="dateType" name="dateType" value="<?php echo $row['DateType']; ?>" />
                        <input type="hidden" id="EmployeeId" name="EmployeeId" value="<?php echo $row['EmployeeId']; ?>" />
                        <input type="hidden" id="icard" name="icard[<?php echo $count;?>]" value="<?php echo $row['TimeCardId']; ?>" />
                    </tr>                    
                <?php
                $count++;
                }
                ?>
                <input type="hidden" id="EndTime" name="count" value="<?php echo $count; ?>" />
            </table>
        </div>
        <input type="submit" value="Save" name="btnSubmit" id="submit" />
        </form>
         <?php
        }
        else{
        ?>
        <div id="content-1" class="contentscroll" > 
            <table border="1">
                <tr id="th">
                    <th width="3%"><?php echo $this->lang->line('tc_ID'); ?></th>
                    <th><?php echo $this->lang->line('tc_Date'); ?></th>
                    <th><?php echo $this->lang->line('tc_Weekday'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualArrivalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualLeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_TrimmedArrivalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_TrimmedLeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WorkingHours'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime1'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime2'); ?></th>
                    <th><?php echo $this->lang->line('tc_RestTime3'); ?></th>
                    <th><?php echo $this->lang->line('tc_ActualWorkHours'); ?></th>
                    <th><?php echo $this->lang->line('tc_LeaveTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayNormalTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekdayLateOverTime'); ?></th>        
                    <th><?php echo $this->lang->line('tc_WeekendOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_WeekendLateOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_HolidayOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_HolidayLateOverTime'); ?></th>
                    <th><?php echo $this->lang->line('tc_LateArrival'); ?></th>
                    <th><?php echo $this->lang->line('tc_EarlyArrival'); ?></th>
                <tr> 
            </table>
            <p><?php echo $this->lang->line('LR_null'); ?></p>
        </div>
        <?php
        }
    ?> 
</div>
<script>
$(".integer").numeric(false, function() {this.value = ""; this.focus(); });
</script>